﻿using Bussiness.DEV;
using Model;
using System;
using System.Collections.Generic;

namespace Bussiness
{
    public class AELTA_PLC : IDEV
    {
        public Device Device { get; set; }
        public List<DevicePOI> DevicePOIList { get; set; }
        Aeltalib _aeltalib;

        public Result Connect()
        {
            Result result = new Result();
            _aeltalib = new Aeltalib();
            string args = Device.Port+",9600,7,2,1,";
            bool _result =_aeltalib.Open(args);
            result.Success = _result;
            if (!_result)
            {
                result.Msg = "开启串口失败";
            }

            return result;
        }

        public Result<List<Cfg_Field>> Read()
        {
            Result<List<Cfg_Field>> result = new Result<List<Cfg_Field>>();
            foreach (var devPOI in DevicePOIList)
            {
                Cfg_Field cfg_Field = new Cfg_Field();
                cfg_Field.name = devPOI.DevPOIAlias;
                switch ((DevPOIType)devPOI.DevPOIType)
                {
                    case DevPOIType.data_int:
                        ushort[] arr = _aeltalib.ReadRegister(0x03, "D"+devPOI.Address, 1);
                        if (arr != null && arr.Length > 0)
                        {
                            result.Success = true;
                            cfg_Field.value = arr[0].ToString();
                        }
                        
                        break;
                    case DevPOIType.data_float:
                        ushort[] arr1 = _aeltalib.ReadRegister(0x03, "D" + devPOI.Address, 1);
                        ushort[] arr2 = _aeltalib.ReadRegister(0x03, "D" + (Convert.ToInt32(devPOI.Address) + 1), 1);

                        if (arr1 == null || arr1.Length != 1)
                        {
                            result.Msg = "读取错误" + (arr1 == null ? " 空" : arr1.Length.ToString());
                            continue;
                        }

                        if (arr2 == null || arr2.Length != 1)
                        {
                            result.Msg = "读取错误" + (arr2 == null ? " 空" : arr2.Length.ToString());
                            continue;
                        }

                        result.Success = true;
                        ushort[] arr3 = new ushort[2] { arr1[0], arr2[0] };

                        byte[] buffer = new byte[4];
                        byte[] buf1 = BitConverter.GetBytes(arr1[0]);
                        byte[] buf2 = BitConverter.GetBytes(arr2[1]);
                        buffer[3] = buf1[1];
                        buffer[2] = buf1[0];
                        buffer[1] = buf2[1];
                        buffer[0] = buf2[0];

                        float value = BitConverter.ToSingle(buffer, 0);
                        cfg_Field.value = value.ToString();
                        break;
                }

                result.Data.Add(cfg_Field);
            }

            return result;
        }
    }
}
